/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package radio_GUI;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.Query;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import model.*;
import model.DBManager;

/**
 *
 * @author TASOS
 */
public class PinakasAlbumMusicGroup extends javax.swing.JFrame {

    /**
     * Creates new form PinakasAlbumMusicGroup
     */
    public PinakasAlbumMusicGroup() {
        
        initComponents();
        Iterator itr = this.albumList1.iterator();
        
        ArrayList<Album> albums2=new ArrayList();
        while(itr.hasNext()){
            model.Album a1=(model.Album)itr.next();
            
            if (a1.getAlbumMgList().size()>0){
                albums2.add(a1);
                //this.albumList1.remove(a1);
            }
        }
        itr = albums2.iterator();
        this.albumList1.clear();
        while(itr.hasNext()){
            model.Album a1=(model.Album)itr.next();
            this.albumList1.add(a1);
        }
        jTable1.repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        radioPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("radioPU").createEntityManager();
        albumQuery = java.beans.Beans.isDesignTime() ? null : radioPUEntityManager.createQuery("SELECT a FROM Album a ");
        albumList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : albumQuery.getResultList();
        albumMgQuery = java.beans.Beans.isDesignTime() ? null : radioPUEntityManager.createQuery("SELECT a FROM AlbumMg a");
        albumMgList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : albumMgQuery.getResultList();
        albumQuery1 = java.beans.Beans.isDesignTime() ? null : radioPUEntityManager.createQuery("SELECT a FROM Album a");
        albumList1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : albumQuery1.getResultList();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, albumList1, jTable1);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${title}"));
        columnBinding.setColumnName("Title");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${mpcId}"));
        columnBinding.setColumnName("Mpc Id");
        columnBinding.setColumnClass(model.Musicproductioncompany.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${albumArtistList}"));
        columnBinding.setColumnName("Album Artist List");
        columnBinding.setColumnClass(java.util.List.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${disknumber}"));
        columnBinding.setColumnName("Disknumber");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${type}"));
        columnBinding.setColumnName("Type");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${releasedate}"));
        columnBinding.setColumnName("Releasedate");
        columnBinding.setColumnClass(java.util.Date.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("Άλμπουμ Συγκροτημάτων");

        jButton1.setText("Νέο");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Επεξεργασία");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Διαγραφή");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Πίσω");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(95, 95, 95))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton4))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2, jButton3, jButton4});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(27, 27, 27)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        //EditMusicGroupAlbum ema = new EditMusicGroupAlbum(null, this);
        EditMusicGroupAlbum_1 ema = new EditMusicGroupAlbum_1(null, this);
        //ema.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //ema.setVisible(true); //show the form;
        ema.show();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        int s=jTable1.getSelectedRow();
        if (s>=0){//αν επέλεξα κάτι από τα καλλιτέχνη
            Album a = (Album)albumList1.get(s);
            
            //EditMusicGroupAlbum ema = new EditMusicGroupAlbum(a, this);
            EditMusicGroupAlbum_1 ema = new EditMusicGroupAlbum_1(a, this);
            //ema.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            ema.setVisible(true); //show the form;
        }
        else{
            JOptionPane.showMessageDialog(null, "Δεν επιλέξατε κάποιο άλμπουμ", "Λάθος", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        this.dispose();
        new MainMenu().setVisible(true);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        int s=jTable1.getSelectedRow();
        if (s>=0){//αν επέλεξα κάτι από τα προϊόντα            
            Album a = albumList1.get(s);
            if (JOptionPane.showConfirmDialog(null, "Είστε σίγουρος ότι θέλετε να διαγράψετε το επιλεγμένο Άλμπουμ;", "Διαγραφή", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
                DBManager.getEm().getTransaction().begin();//έναρξη transaction  με τη βάση
                try{
                    a.setAlbumMgList(null);
                    
                    Query q;
                    
                    Iterator itr=a.getSongList().iterator();
                    
                    while (itr.hasNext()){
                        Song s1=(Song)itr.next();
                        q = DBManager.getEm().createQuery("DELETE FROM Song s WHERE s.songId="+s1.getSongId(), Song.class);
                        q.executeUpdate();
                    }
                    q = DBManager.getEm().createQuery("DELETE FROM AlbumMg amg WHERE amg.albumId.albumId="+a.getAlbumId(), model.AlbumMg.class);
                    q.executeUpdate();
                    
                    
                    q = DBManager.getEm().createQuery("DELETE FROM Album a WHERE a.albumId="+a.getAlbumId(), Album.class);

                    q.executeUpdate();

                    DBManager.getEm().getTransaction().commit();//πραγματοποίηση αλλαγών στη βάση
                    albumList1.remove(s);
                    jTable1.repaint();

                } catch (Exception e) {
                    e.printStackTrace();
                    DBManager.getEm().getTransaction().rollback();
                }
            }
        }
        else{
            JOptionPane.showMessageDialog(null, "Δεν επιλέξατε κάποιο άλμπουμ", "Λάθος", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PinakasAlbumMusicGroup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PinakasAlbumMusicGroup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PinakasAlbumMusicGroup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PinakasAlbumMusicGroup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DBManager.openConnection();//Ksekina ton entity manager gia test                
                new PinakasAlbumMusicGroup().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<model.Album> albumList;
    private java.util.List<model.Album> albumList1;
    private java.util.List<model.AlbumMg> albumMgList;
    private javax.persistence.Query albumMgQuery;
    private javax.persistence.Query albumQuery;
    private javax.persistence.Query albumQuery1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.persistence.EntityManager radioPUEntityManager;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
